package com.ruoyi.web.controller.applet;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.*;
import com.ruoyi.system.domain.vo.BdmRoyaltyRecordVo;
import com.ruoyi.system.domain.vo.BdmUserTeamVo;
import com.ruoyi.system.domain.vo.applet.BdmRoyaltyRecordOrderVo;
import com.ruoyi.system.service.IBdmRoyaltyRecordService;
import com.ruoyi.system.service.IBdmTopupRecordService;
import com.ruoyi.system.service.IBdmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分销提成记录Controller
 *
 * @author ruoyi
 * @date 2024-12-09
 */
@Api(tags = "小程序-推广中心")
@RestController
@RequestMapping("/applet/royalty")
public class AppletRoyaltyRecordController extends BaseController {

    @Autowired
    private IBdmRoyaltyRecordService bdmRoyaltyRecordService;

    @Autowired
    private IBdmTopupRecordService bdmTopupRecordService;

    @Autowired
    private IBdmUserService bdmUserService;

    /**
     * 小程序推广收益列表
     * @param dto
     * @return
     */
    @ApiOperation(value = "获取分销提成记录列表")
    @GetMapping("/getRoyaltyRecordPage")
    public TableDataInfo getRoyaltyRecordPage(BdmRoyaltyRecordDto dto) {
        dto.setUserId(SecurityUtils.getUserId());
        startPage();
        dto.setQueryType(1);
        List<BdmRoyaltyRecordVo> list = bdmRoyaltyRecordService.getRoyaltyRecordPage(dto);
        return getDataTable(list);
    }

    /**
     * 推广收益详情
     * @param dto
     * @return
     */
    @GetMapping("/selectRoyaltyRecord")
    public TableDataInfo selectRoyaltyRecord(BdmRoyaltyRecordDto dto) {
        dto.setUserId(SecurityUtils.getUserId());
        startPage();
        List<BdmRoyaltyRecordOrderVo> bdmRoyaltyRecordOrderVos = bdmRoyaltyRecordService.selectRoyaltyRecord(dto);
        return getDataTable(bdmRoyaltyRecordOrderVos);
    }


    /**
     * 小程序推广统计
     * @return
     */
    @GetMapping("/getStatistics")
    public AjaxResult getStatistics() {
        return success(bdmRoyaltyRecordService.getStatistics());
    }

    /**
     * 小程序推广中心提现
     * @param bdmTopupRecordDto
     * @return
     */
    @Log(title = "小程序推广中心提现", businessType = BusinessType.INSERT)
    @PostMapping("/withdrawal")
    public AjaxResult appletWithdrawal(@Valid @RequestBody BdmTopupRecordDto bdmTopupRecordDto)
    {
        try {
            Long.parseLong(bdmTopupRecordDto.getAccountNumber());
        } catch (NumberFormatException e) {
            throw new RuntimeException("请输入数字");
        }
        return toAjax(bdmTopupRecordService.insertBdmTopupRecord(bdmTopupRecordDto));
    }

    /**
     * 小程序我的客户
     * @return
     */
    @GetMapping("/mineGroup")
    public AjaxResult mineGroup(UserGroupDto userGroupDto) {
        return success(bdmUserService.userGroup(userGroupDto));
    }

    /**
     * 查询其他用户团队
     * @param userGroupDto
     * @return
     */
    @GetMapping("/elseGroup")
    public TableDataInfo elseGroup(UserGroupDto userGroupDto){
        startPage();
        return getDataTable(bdmUserService.elseGroup(userGroupDto));
    }

    /**
     * 新小程序我的客户
     *
     * @param dto
     * @return {@link BdmUserTeamVo }
     */
    @GetMapping("/mineGroupNew")
    public TableDataInfo mineGroupNew(BdmUserTeamDto dto) {
        startPage();
        dto.setUserId(SecurityUtils.getUserId());
        return getDataTable(bdmUserService.getTeamNumber(dto));
    }

    /**
     * 新查询其他用户团队
     *
     * @param dto
     * @return
     */
    @GetMapping("/elseGroupNew")
    public TableDataInfo elseGroupNew(BdmUserTeamDto dto) {
        startPage();
        dto.setFlag(SecurityUtils.getUserId());
        return getDataTable(bdmUserService.getTeamNumber(dto));
    }

    /**
     * 查询团队
     * @param dto
     * @return
     */
    @GetMapping("/findUserTeam")
    public AjaxResult findUserTeam(BdmUserTeamDto dto) {
        dto.setUserId(SecurityUtils.getUserId());
//        dto.setUserId(175037L);
        return bdmUserService.findUserTeam(dto);
    }

    @GetMapping("/getUserSuperior")
    public AjaxResult getUserSuperior(BdmUserTeamDto dto) {
        dto.setUserId(SecurityUtils.getUserId());
        return bdmUserService.getUserSuperior(dto);
    }
}
